package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wit.witmg.entity.TSoftware;
import com.wit.witmg.entity.TSystemcert;
import com.wit.witmg.mapper.TSystemcertMapper;
import com.wit.witmg.service.ITSystemcertService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.service.OSSService;
import com.wit.witmg.util.DateUtil;
import com.wit.witmg.util.JsonResult;
import com.wit.witmg.util.StringUtils;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;

/**
 * <p>
 * 证书信息表 服务实现类
 * </p>
 *
 * @author bob
 * @since 2025-09-19
 */
@Service
public class TSystemcertServiceImpl extends ServiceImpl<TSystemcertMapper, TSystemcert> implements ITSystemcertService {
    @Resource
    private OSSService ossService;

    @Override
    public Object add(HttpServletRequest request, String json, MultipartFile pdf, MultipartFile file) {
        TSystemcert t= JSONObject.parseObject(json,TSystemcert.class);
        if(pdf!=null){
            String prefix = StringUtils.instance.getPrefix(pdf.getOriginalFilename());//后缀名
            String url= ossService.uploadFile(pdf, t.getNa()+"."+prefix,"patent");
            t.setPdf("https://witknowpublic.oss-cn-hangzhou.aliyuncs.com/"+url);
        }
        if(file!=null){
            try {
                String prefix = StringUtils.instance.getPrefix(file.getOriginalFilename());//后缀名
                String rootPath = StringUtils.instance.getDir(request, 7);
                String fileName = DateUtil.formatDate(new Date(), "yyyyMMdd_HHmmssSSS") + "." + prefix;
                String fileTempName ="temp_"+ fileName;
                String imgThumbPath=rootPath+fileTempName;
                BufferedImage bufferedImage = ImageIO.read(file.getInputStream()); // 通过MultipartFile得到InputStream，从而得到BufferedImage
                if(bufferedImage!=null) {
                    int nw = bufferedImage.getWidth();
                    int nh = bufferedImage.getHeight();
                    double iwh = (double) nw / (double) nh;
                    int smin = nw < nh ? nw : nh;
                    int ih, iw;
                    int outw;
                    int outh;
                    if(smin>1024) {
                        if (iwh > 1) {
                            ih = 1024;
                            iw = (int) (1024 * iwh);
                        } else {
                            iw = 1024;
                            ih = (int) (1024 / iwh);
                        }
                        outw=iw;
                        outh=ih;
                    }else{
                        outw=nw;
                        outh=nh;
                    }
                    File newFile = new File(rootPath + fileName);
                    file.transferTo(newFile);// 将内存中的数据写入磁盘
                    //生成缩略图
                    Thumbnails.of(newFile).forceSize(outw, outh).outputQuality(0.8).toFile(imgThumbPath);
                    File tempFile=new File(imgThumbPath);
                    MultipartFile cMultiFile = new MockMultipartFile("file", t.getNu(), null, new FileInputStream(tempFile));
                    String url = ossService.uploadFile(cMultiFile, t.getNu() + "." + prefix, "patent");
                    t.setImg("https://witknowpublic.oss-cn-hangzhou.aliyuncs.com/" + url);
                    newFile.delete();
                    tempFile.delete();
                }

            }catch (Exception e){}
        }
        if(t.getId()!=null && t.getId()>0){//修改
            boolean bl= updateById(t);
            if(!bl){
                return JsonResult.error("修改失败");
            }
        }else{
            boolean bl=save(t);
            if(!bl){
                return JsonResult.error("保存失败");
            }
        }
        return JsonResult.success(t);
    }

    @Override
    public Object get(HttpServletRequest request, String pro, String na, String nu, String tp) {
        LambdaQueryWrapper<TSystemcert> qw=new LambdaQueryWrapper<>();
        if(pro!=null){
            qw.like(TSystemcert::getPro,pro);
        }
        if(na!=null){
            qw.like(TSystemcert::getNa,na);
        }
        if(nu!=null){
            qw.eq(TSystemcert::getNu,nu);
        }
        if(tp!=null){
            qw.eq(TSystemcert::getTp,tp);
        }
        qw.orderByDesc(TSystemcert::getDate);
        return JsonResult.success(list(qw));
    }
}
